package cn.tedu.micro.demo.cart.controller;

import cn.tedu.micro.demo.cart.service.CartService;
import cn.tedu.micro.demo.common.pojo.vo.CartVO;
import cn.tedu.micro.demo.common.protocol.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 专门负责 购物车相关业务模块接口
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    @PostMapping("add")
    public JsonResult<Boolean> saveCart(Integer userId, String productCode, Integer count) {
        cartService.saveCart(userId, productCode, count);
        return JsonResult.ok("ok", true);
    }

    @GetMapping("list")
    public JsonResult<List<CartVO>> select(Integer userId) {
        List<CartVO> list = cartService.selectCartList(userId);
        return JsonResult.ok("ok", list);
    }

    @PostMapping("delete")
    public JsonResult<Void> deleteCart(@RequestParam("userId") Integer userId, @RequestParam("productCode") String productCode) {
        cartService.deleteCart(userId, productCode);
        return JsonResult.ok("ok",null);
    }

}
